package art.server.services;

import java.util.ArrayList;
import java.util.List;

import art.client.services.ArtService;
import art.server.EntityConverter;
import art.server.db.DatabaseHandler;
import art.server.db.entities.Art;
import art.server.db.entities.Genre;
import art.server.db.entities.Rating;
import art.shared.entities.ArtShared;
import art.shared.entities.GenreShared;
import art.shared.entities.RatingShared;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ArtServiceImpl extends RemoteServiceServlet implements ArtService {

	private static final long serialVersionUID = 1L;

	@Override
	public List<ArtShared> getArts() {
		List<Art> arts = DatabaseHandler.getInstance().getAllArts();
		List<ArtShared> artsShared = new ArrayList<ArtShared>();
		if (arts != null && !arts.isEmpty()) {
			for (Art art : arts) {
				artsShared.add(EntityConverter.getConverter()
						.convertArtToShared(art));
			}
		}
		return artsShared;
	}

	@Override
	public boolean addArt(ArtShared art) {
		return DatabaseHandler.getInstance().saveArt(
				EntityConverter.getConverter().convertArt(art));
	}

	@Override
	public boolean deleteArt(Long id) {
		return DatabaseHandler.getInstance().removeArt(id);
	}

	@Override
	public List<ArtShared> getArtsOfUser(Long userId) {
		List<Art> arts = DatabaseHandler.getInstance().getArtsByUser(userId);
		List<ArtShared> artsShared = new ArrayList<ArtShared>();
		if (arts != null && !arts.isEmpty()) {
			for (Art art : arts) {
				artsShared.add(EntityConverter.getConverter()
						.convertArtToShared(art));
			}
		}
		return artsShared;
	}

	@Override
	public boolean addRatingToArt(RatingShared rating) {
		return DatabaseHandler.getInstance().saveRating(
				EntityConverter.getConverter().convertRating(rating));
	}

	@Override
	public List<GenreShared> getGenres() {
		List<Genre> genres = DatabaseHandler.getInstance().getAllGenres();
		List<GenreShared> genresShared = new ArrayList<GenreShared>();
		if (genres != null && !genres.isEmpty()) {
			for (Genre genre : genres) {
				genresShared.add(EntityConverter.getConverter()
						.convertGenreToShared(genre));
			}
		}
		return genresShared;
	}

	@Override
	public List<RatingShared> getRatingsOfArt(long artId) {
		List<Rating> ratings = DatabaseHandler.getInstance().getRatingsByArt(
				artId);
		List<RatingShared> ratingsShared = new ArrayList<RatingShared>();
		if (ratings != null && !ratings.isEmpty()) {
			for (Rating rating : ratings) {
				ratingsShared.add(EntityConverter.getConverter()
						.convertRatingToShared(rating));
			}
		}
		return ratingsShared;
	}
	
	@Override
	public List<RatingShared> getAllRatings() {
		List<Rating> ratings = DatabaseHandler.getInstance().getAllRatings();
		List<RatingShared> ratingsShared = new ArrayList<RatingShared>();
		if (ratings != null && !ratings.isEmpty()) {
			for (Rating rating : ratings) {
				ratingsShared.add(EntityConverter.getConverter()
						.convertRatingToShared(rating));
			}
		}
		return ratingsShared;
	}
}
